package com.andy.blog.integration.service.impl;

import com.andy.blog.integration.service.MailIntegrationService;
import com.andy.blog.integration.vo.SimpleMailVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * Created by andy on 2023/7/31 17:10
 */
@Service
public class MailIntegrationServiceImpl implements MailIntegrationService {

    @Resource
    private JavaMailSender mailSender;

    @Value("${spring.mail.from}")
    private String from;


    @Override
    public void sendSimpleMail(SimpleMailVO simpleMailVo) {

        if (CollectionUtils.isEmpty(simpleMailVo.getReceiver())) {
            return;
        }

        // 准备发送通知邮件对象
        //log.debug("准备发送通知邮件对象。");
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(simpleMailVo.getReceiver().toArray(new String[0]));
        //邮件主题
        message.setSubject(simpleMailVo.getSubject());
        //邮件内容
        message.setText(simpleMailVo.getContent());
        message.setSentDate(new Date());
        //发送邮件
        mailSender.send(message);
    }

    @Override
    public void sendAttachmentsMail(SimpleMailVO simpleMailVO) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(simpleMailVO.getReceiver().toArray(new String[0]));
            helper.setSubject(simpleMailVO.getSubject());
            helper.setText(simpleMailVO.getContent(), true);

            FileSystemResource file = new FileSystemResource(new File(simpleMailVO.getFilePath()));
            String fileName = simpleMailVO.getFilePath().substring(simpleMailVO.getFilePath().lastIndexOf(File.separator));
            helper.addAttachment(fileName, file);
            mailSender.send(message);
            //log.info("邮件已经发送。");
        } catch (MessagingException e) {
            //log.error("发送邮件时发生异常！", e);
        }
    }

    @Override
    public void sendHtmlMail(SimpleMailVO htmlMailVo) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            //邮件发送人
            messageHelper.setFrom(from);
            //邮件接收人
            messageHelper.setTo(htmlMailVo.getReceiver().toArray(new String[0]));
            //邮件主题
            message.setSubject(htmlMailVo.getSubject());
            //邮件内容，html格式
            messageHelper.setText(htmlMailVo.getContent(), true);
            //发送
            mailSender.send(message);
            //日志信息
            //log.info("邮件已经发送。。。");
        } catch (Exception e) {
            e.printStackTrace();
            //log.error("邮件发送失败！", e);
            //log.error(e.getMessage());
        }
    }
}
